'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:MaterialList
 * @description
 * # MaterialList
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('MaterialList', function ($scope, Util, openApi,localStorageService, PagingService) {

        $scope.title = "材料"
        // 选择后回传的数据
        $scope.choosePeople = function (index) {
            var item = $scope.taskList.items[index]
            item.selected = !item.selected;
            // $scope.submit(index);
            $scope.selectLists = getSelectedData();
        };
        var getPropertyValue = function () {
            return $scope.selectLists.map(function (item, index) {
                return {
                    listId: item.pkId,
                    pkId: item.pkId,
                    fileNo: item.listNo,
                    spec: item.spec,
                    name: item.name,
                    brand: item.brand,
                    unit: item.unit,
                    price: item.price || 0,
                    soruceType: 1
                }
            })
        }
        var getSelectedData = function () {
            var selectedData = $scope.taskList.items.filter(function (item, index) {
                return item.selected;
            });
            return selectedData
        }
        $scope.submit = function () {
            if ($scope.selectLists.length < 1) {
                Util.Toast("请选择材料" );
                return
            }
            var selectList = getPropertyValue()
            localStorageService.set("materialslist",selectList)
            Util.goBack();
        }

        $scope.search = function (name) {
            var param = {};
            param.name = name;
            $scope.taskList = new PagingService(openApi.materialNormlist, param);
        }
        $scope.search();
    });